﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Threading;

namespace BrainTechLLC.WPF
{
    public class MouseTracker : BaseNotifyPropertyChanged
    {
        public double _StartX;
        public double _StartY;
        public double _CurrentX;
        public double _CurrentY;
        public double _EndX;
        public double _EndY;
        public int _Action;
        public List<DependencyObject> _HitTestResults = new List<DependencyObject>();

        public string DisplayCoords
        {
            get
            {
                return string.Format("{0},{1} / {2},{3} / {4},{5}", _StartX, _StartY, _CurrentX, _CurrentY, _EndX, _EndY) + "  " + HitTestResults.BuildList(", ", d => InterpretDependencyObject(d));
            }
        }

        public HitTestResultBehavior MyHitTestResult(HitTestResult result)
        {
            // Add the hit test result to the list that will be processed after the enumeration.
            HitTestResults.Add(result.VisualHit);

            // Set the behavior to return visuals at all z-order levels.
            return HitTestResultBehavior.Continue;
        }

        public string InterpretDependencyObject(DependencyObject d)
        {
            Grid g = d as Grid;
            ScrollViewer s = d as ScrollViewer;
            Border b = d as Border;
            System.Windows.Controls.Image i = d as System.Windows.Controls.Image;

            if (i != null)
            {
                return "Image (" + i.Name + ")";
            }
            if (g != null)
            {
                return "Grid (" + g.Name + ")";
            }
            if (s != null)
            {
                return "ScrollViewer (" + s.Name + ")";
            }
            if (b != null)
            {
                return "Border (" + b.Name + ")";
            }
            return d.GetType().Name;
        }

        public List<DependencyObject> HitTestResults
        {
            get { return _HitTestResults; }
            set { _HitTestResults = value; }
        }

        public double StartX
        {
            get { return _StartX; }
            set { SetValue(() => _StartX, value); }
        }

        public double StartY
        {
            get { return _StartY; }
            set { SetValue(() => _StartY, value); }
        }
        public double CurrentX
        {
            get { return _CurrentX; }
            set { SetValue(() => _CurrentX, value); }
        }
        public double CurrentY
        {
            get { return _CurrentY; }
            set { SetValue(() => _CurrentY, value); }
        }
        public double EndX
        {
            get { return _EndX; }
            set { SetValue(() => _EndX, value); }
        }
        public double EndY
        {
            get { return _EndY; }
            set { SetValue(() => _EndY, value); }
        }
    }
}